package com.wust.dns_resolver.controller;

import com.wust.dns_resolver.service.DnsService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
@Slf4j
@RequestMapping("/common")
public class DnsController {

    @Autowired
    private DnsService dnsService;

    /**
     * 根据用户输入的域名，查询对应的IP地址
     * @param domain 用户输入的域名
     * @param isIPv6 是否解析IPv6地址
     * @return 域名对应的IP地址
     */
    @GetMapping("/resolve")
    public String resolveDomain(@RequestParam String domain, @RequestParam(defaultValue = "false") boolean isIPv6) {
        log.info("wuster-windchimes的DNS_resolver已接收到域名：{}，解析类型：{}", domain, isIPv6 ? "IPv6" : "IPv4");
        return dnsService.getIpAddress(domain, isIPv6);
    }

}
